package com.kanshu.client.ui.activity.ipc;

import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;

import com.kanshu.basictest.ICompute;
import com.kanshu.basictest.ISecurityCenter;
import com.kanshu.client.R;
import com.kanshu.client.constants.BinderPoolConstants;
import com.kanshu.client.model.menu.MenuItem;
import com.kanshu.client.ui.base.BaseMenuActivity;

import java.util.ArrayList;
import java.util.List;

public class BinderPoolTestActivity extends BaseMenuActivity {

    private static final String TAG = "BinderPoolTestActivity";


    @Override
    protected String provideBarTitle() {
        return "Binder池测试";
    }

    @Override
    protected List<? extends MenuItem> provideMenuData() {
        List<MenuItem> menuItems = new ArrayList<>();
        menuItems.add(0, new MenuItem("先加密后解密", R.color.orange_200));
        menuItems.add(1, new MenuItem("求和", R.color.orange_200));
        return menuItems;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        switch (position) {
            case 0:
                encryptAfterDecrypt("zhangsan");
                break;
            case 1:
                add(12, 23);
                break;
            default:
                break;
        }
    }

    @Override
    protected void init() {
        super.init();

    }

    /**
     * 先加密后解密
     *
     * @param content 初始明文
     */
    private void encryptAfterDecrypt(final String content) {
        new Thread() {
            @Override
            public void run() {
                super.run();
                BinderPoolHelper mBinderPoolHelper = BinderPoolHelper.getInstance(BinderPoolTestActivity.this);
                IBinder service = mBinderPoolHelper.queryBinder(BinderPoolConstants.BINDER_SECURITY_CENTER);
                ISecurityCenter securityCenter = ISecurityCenter.Stub.asInterface(service);
                try {
                    String encryptResult = securityCenter.encrypt(content);
                    Log.i(TAG, "encrypt: 加密前：" + content + ",加密后：" + encryptResult);

                    String decryptResult = securityCenter.decrypt(encryptResult);
                    Log.i(TAG, "decrypt: 解密前：" + encryptResult + ",解密后：" + decryptResult);

                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }


    private void add(final int a, final int b) {
        new Thread() {
            @Override
            public void run() {
                super.run();
                BinderPoolHelper mBinderPoolHelper = BinderPoolHelper.getInstance(BinderPoolTestActivity.this);
                IBinder service = mBinderPoolHelper.queryBinder(BinderPoolConstants.BINDER_COMPUTE);
                ICompute compute = ICompute.Stub.asInterface(service);
                try {
                    int result = compute.add(a, b);
                    Log.i(TAG, "add: " + a + "+" + b + "=" + result);
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }


}
